//
//  UPOrderProductCell.m
//  LoveFreshBee
//
//  Created by wyzc on 16/6/13.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//

#import "UPOrderProductCell.h"
#import "UPOrderProductModel.h"
#import <UIImageView+WebCache.h>
#import "UPShopingCartMOdel.h"
#import "UPShopingCart.h"

@interface UPOrderProductCell ()
@property (weak, nonatomic) IBOutlet UIImageView *leftImageView;
//精选
@property (weak, nonatomic) IBOutlet UILabel *selectLable;
//名称
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
//买一赠一
@property (weak, nonatomic) IBOutlet UILabel *buyOneSendOnelabel;
//规格
@property (weak, nonatomic) IBOutlet UILabel *specifyLabel;
//价格
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
//市场价格
@property (weak, nonatomic) IBOutlet UILabel *marketPriceLabe;
//减号
@property (weak, nonatomic) IBOutlet UIButton *reducebutton;
//购买数量
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
//加号
@property (weak, nonatomic) IBOutlet UIButton *increaseButton;

@end

@implementation UPOrderProductCell

-(void)setModel:(UPOrderProductModel *)model
{
    _model=model;
   // self.buyOneSendOnelabel.hidden=!model.pm_desc.length;
    self.buyOneSendOnelabel.text=model.pm_desc;
    self.nameLabel.text=model.name;
    self.specifyLabel.text=model.specifics;
    
    //self.priceLabel.text=[NSString stringWithFormat:@"￥%.1f",model.price];
    
    
    NSString *priceStr=[NSString stringWithFormat:@"￥%.1f",model.price];
    self.priceLabel.text=priceStr;
    
    NSString *marketPriceStr=[NSString stringWithFormat:@"￥%g",model.market_price];
    
    self.marketPriceLabe.text=marketPriceStr;
    //self.marketPriceLabe.text=[NSString stringWithFormat:@"￥%g",model.market_price];
    
    
    //创建一个属性字符串，并设置属性---设置删除线
    
    NSAttributedString *attrPrice=[[NSAttributedString alloc]initWithString:marketPriceStr attributes:@{NSStrikethroughColorAttributeName:UPColor(169, 169, 169),NSStrikethroughStyleAttributeName:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle)}];
    
    self.marketPriceLabe.attributedText=attrPrice;
    UIImage *holderImage=[UIImage imageNamed:@"v2_placeholder_square"];
    NSURL *url=[NSURL URLWithString:model.img];
    
    [self.leftImageView sd_setImageWithURL:url placeholderImage:holderImage];
    
    
    
    //判断该模型的主键在购物车中是否存在，如果存在的话，就显示—号按钮和购买数量，否则不显示
    //获取购物车
    UPShopingCart *shopingCart=[UPShopingCart sharedCart];
    //获取当前cell所对应的模型主键
    int productId=[model.product_id intValue];
    //遍历购物车中每一个模型的主键
    for (UPShopingCartMOdel *shopModel in shopingCart.cart) {
        //判断购物车中模型的主键是否和当前cell所对应模型的主键是否一致
        if (shopModel.iid==productId) {
            //显示加号按钮，减号按钮，数量标签
            self.reducebutton.hidden=YES;
            self.numberLabel.hidden=YES;
            self.numberLabel.text=[NSString stringWithFormat:@"%d",shopModel.num];
            return;
        }
    }
    //隐藏减号按钮和数字标签
    self.reducebutton.hidden=YES;
    self.numberLabel.hidden=YES;
    self.numberLabel.text=@"0";
    //设置+的外圈消失
    [self.increaseButton setBackgroundImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    }

- (void)awakeFromNib {
    [super awakeFromNib];
    CALayer *layer=self.selectLable.layer;
    layer.borderWidth=1;
    layer.borderColor=[UIColor redColor].CGColor;
    layer.cornerRadius=5;
    
    
    layer=self.buyOneSendOnelabel.layer;
    layer.borderWidth=1;
    layer.masksToBounds=YES;//裁掉超过底层的部分
    layer.borderColor=[UIColor redColor].CGColor;
    layer.cornerRadius=5;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

//加号按钮点击事件
- (IBAction)btnPlusClick:(id)sender {
    UIButton *plusBtn=(UIButton *)sender;
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"v2_increased"] forState:UIControlStateNormal];
    
    
    self.reducebutton.hidden=NO;
    self.numberLabel.hidden=NO;
    int num=[self.numberLabel.text intValue]+1;
    
    plusBtn.enabled=num<self.model.store_nums;
    self.numberLabel.text=@(num).stringValue;
    [self.reducebutton setBackgroundImage:[UIImage imageNamed:@"v2_reduced"] forState:UIControlStateNormal];
    
    

    //把商品模型转换成购物模型
    UPShopingCartMOdel *model=[UPShopingCartMOdel shopingModelWithProductModel:self.model];
    
    //获取购物车
    UPShopingCart *shopingCart=[UPShopingCart sharedCart];
    
    //添加到购物车
    [shopingCart addToCart:model];
    
}


//减号按钮点击事件
- (IBAction)btnSubClick:(id)sender {
    UIButton *subBtn=(UIButton *)sender;
    self.increaseButton.enabled=YES;
    
    int num=[self.numberLabel.text intValue]-1;
//    if (num>0) {
//        self.numberLabel.text=@(num).stringValue;
//    }else{
//        self.reducebutton.hidden=YES;
//        self.numberLabel.hidden=YES;
    subBtn.hidden=!num;
    self.numberLabel.hidden=!num;
    self.numberLabel.text=@(num).stringValue;
    if (num<=0) {
        [self.increaseButton setBackgroundImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    }
    
    
    //把商品模型转换成购物模型
   // UPShopingCartMOdel *model=[UPShopingCartMOdel shopingModelWithProductModel:self.model];
    
    //获取购物车
    UPShopingCart *shopingCart=[UPShopingCart sharedCart];
    
    //
    
    int iid=[self.model.product_id intValue];
    
    [shopingCart removeFromCart:iid];

    
}

@end
